package com.fenghuajueli.libbasecoreui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 用于显示用户协议和隐私政策
 */
public class RichTextView extends androidx.appcompat.widget.AppCompatTextView {
    public RichTextView(Context context) {
        super(context);
    }

    public RichTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RichTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void loadUrl(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        getContent(path);
    }

    private void getContent(String path) {
        new Thread() {
            @Override
            public void run() {
                try {
                    URL url = new URL(path);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setConnectTimeout(30000);
                    con.setRequestMethod("GET");
                    int code = con.getResponseCode();
                    if (code == HttpURLConnection.HTTP_OK) {
                        InputStream is = con.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
                        String result = "";
                        String line = "";
                        try {
                            while ((line = reader.readLine()) != null) {
                                result = result + line;
                            }
                            if (TextUtils.isEmpty(result)) {
                                return;
                            }
                            String content;
                            if (path.contains("an-user-agreement")) {
                                content = result.replace("<title>用户协议</title>", "");
                            } else {
                                int start = result.indexOf("<body");
                                int end = result.indexOf("</body>");
                                content = result.substring(start, end);
                            }
                            Message msg = new Message();
                            msg.what = 1;
                            msg.obj = content;
                            mHandler.sendMessage(msg);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (is != null) {
                                is.close();
                            }
                            if (reader != null) {
                                reader.close();
                            }
                        }
                    }
                    con.disconnect();
                } catch (Exception e) {

                }
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String content = (String) msg.obj;
            setText(Html.fromHtml(content));
        }
    };
}
